<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\index\model\FormworkOrder as FormworkOrderModel;
use think\Session;


/**
 * 用户订单
 */
class Order extends Frontend
{
    protected $layout = 'user';
    protected $noNeedLogin = ['login', 'register', 'wechat'];
    protected $noNeedRight = ['*'];

    /**
     * 订单列表
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $model = new FormworkOrderModel;
        $list = $model->getList($user['id']);
        $this->view->assign('title', __('My formwork'));
        return $this->view->fetch('index',compact('list'));
    }

     /**
     * 小程序管理登录
     */
    public function login($formwork_order_id)
    {   
        $order = FormworkOrderModel::detail($formwork_order_id);
        $session_name = 'hema_'.$order['formwork']['name'];
        $url = '/store/'.$order['formwork']['name'].'/index/index';
        // 验证用户名密码是否正确
        if($user = $this->auth->getUser()){
            // 保存登录状态
            Session::set($session_name, [
                'user' => $user->toArray(),
                'formwork_order_id' => $formwork_order_id,
                'shop_type' => $order['shop_type'],
                'applet_id' => $order['applet_id'],
                'is_login' => true,
            ]);
            $this->success('正在跳转...', $url);
        }else{
            $this->error('登录失败, 用户名或密码错误');
        }
    }

    /**
     * 退出登录
     */
    public function logout($name)
    {
        Session::clear('hema_'.$name);
        $this->redirect(url('order/index'));
    }
   
}
